package com.ygo.feihua;


import android.os.*;
import android.support.v4.app.*;
import android.support.v4.util.*;
import android.util.*;
import android.view.*;
import android.view.animation.*;
import android.widget.*;
import cn.bmob.v3.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.Management.*;
import java.util.*;

import android.support.v4.util.ArrayMap;
import com.ygo.feihua.task.*;
import com.ygo.feihua.console.*;

public class GroupChatFragment extends Fragment implements Task
{

	private boolean isInitChat;

	@Override
	public void execute(int number)
	{
		han.sendEmptyMessage(number);
		// TODO: Implement this method
	}
	
	Handler han=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case INIT_LIAOTIAN:
					//初始化聊天控件
					lv=new LiaotianView(getActivity(),zy_layout);
					//刷新消息Adapter
					lv.sxadp();
					//开启聊天监听
					ltm.startListener();
					break;
			}
		}
		
	};
	private static final int INIT_LIAOTIAN = 2;
	//聊天控件类
	LiaotianView lv;
	//聊天管理类
	LiaotianManagement ltm;
	private LinearLayout zy_layout;
	
	private List<Map<String,Object>> data;
	private TaskConsole tac;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_group_chat,null);
	
		zy_layout=(LinearLayout) v.findViewById(R.id.zy_layout);
		
		
		// TODO: Implement this method
		return v;
	}

	@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) { //相当于Fragment的onResume
            //在这里去请求数据。。。
			if(!isInitChat){
				isInitChat=true;
				//获取聊天管理类对象
				ltm=LiaotianManagement.getdx();
				data=ltm.getSl();
				tac=TaskConsole.getDx();	
				sx();
				
			}
        } else { //相当于Fragment的onPause

        }
    }
	
	private void sx(){
		(((CeHua)getActivity()).getMain_setting()).startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.rotation_repeat));
		BmobQuery<GroupChatMessage> tie=new BmobQuery<GroupChatMessage>();
		tie.setLimit(20);
		tie.order("-createdAt");
	    tie.include("userid");// 希望在查询帖子信息的同时也把发布人的信息查询出来

		tie.findObjects( new FindListener<GroupChatMessage>(){

				@Override
				public void done(List<GroupChatMessage> p1, BmobException p2)
				{
					(((CeHua)getActivity()).getMain_setting()).clearAnimation();
					if(p2==null){
						for(int i=0;i<p1.size();i++){      
							if(p1.get(p1.size()-i-1).getUserid()!=null&&p1.get(p1.size()-i-1).getUserid().getUsername()!=null){
								Map<String,Object> ma=new ArrayMap<String,Object>();
								ma.put("user",p1.get(p1.size()-i-1).getUserid());
								ma.put("id",p1.get(p1.size()-i-1).getUserid().getObjectId());
								ma.put("sj",p1.get(p1.size()-i-1).getCreatedAt());
								ma.put("name",p1.get(p1.size()-i-1).getName());
								ma.put("neirong",p1.get(p1.size()-i-1).getMessage());
								ma.put("userqx",p1.get(p1.size()-i-1).getUserid().getUser_qx());
								ma.put("tx",p1.get(p1.size()-i-1).getUserid().getavatar());
								ma.put("point",p1.get(p1.size()-i-1).getUserid().getUserpoint());
								ma.put("time",p1.get(p1.size()-i-1).getCreatedAt());
								data.add(ma);
							}
						}
					
						tac.addTask(GroupChatFragment.this,INIT_LIAOTIAN);
					}else{
						Log.e("聊天初始化失败","原因为"+p2);
					}
					// TODO: Implement this method
				}
			});


	}
	
	
	
	
	
	
}
